#!/bin/sh
################################################################################
# media
#
# This script mounts the remote media directories.
################################################################################
. /etc/rc.d/functions

start() {
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Starting..."

    if [ -n "${MM_MEDIA_GAME_URL}" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Mounting ${MM_MEDIA_GAME_URL} to ${MM_MEDIA_GAME_MOUNTPOINT}"
        mm_url_mount "${MM_MEDIA_GAME_URL}"    "${MM_MEDIA_GAME_MOUNTPOINT}"
    fi

    if [ "x${MM_OPTICAL_RIP_ENABLED}" = 'xyes' ] ; then
        if [ -n "${MM_OPTICAL_RIP_URL}" ] ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Mounting ${MM_OPTICAL_RIP_URL} to /mnt/optical-rip"
            mm_url_mount "${MM_OPTICAL_RIP_URL}" "/mnt/optical-rip"
        fi
    fi

    if [ -n "${MM_MEDIA_AUDIO_RIP_URL}" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Mounting ${MM_MEDIA_AUDIO_RIP_URL} to /home/minimyth/.mythtv/tmp/RipTemp"
        mm_url_mount "${MM_MEDIA_AUDIO_RIP_URL}" "/home/minimyth/.mythtv/tmp/RipTemp"
    fi

    if [ -n "${MM_MEDIA_SCREENSAVER_GALLERY_URL}" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Mounting ${MM_MEDIA_SCREENSAVER_GALLERY_URL} to ${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}"
        mm_url_mount "${MM_MEDIA_SCREENSAVER_GALLERY_URL}" "${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}"
    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Started with return code=0..."

    return 0
}

stop() {
    mm_message_output info "unmounting media shares ..."

    if [ "x${MM_OPTICAL_RIP_ENABLED}" = 'xyes' ] ; then
        if [ -n "${MM_OPTICAL_RIP_URL}" ] ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Unmounting /mnt/optical-rip"
            /bin/umount "/mnt/optical-rip"
        fi
    fi

    if [ -n "${MM_MEDIA_AUDIO_RIP_URL}" ] ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Unmounting /home/minimyth/.mythtv/tmp/RipTemp"
            /bin/umount "/home/minimyth/.mythtv/tmp/RipTemp"
    fi
 
    if [ -n "${MM_MEDIA_GAME_URL}" ] && [ -d "${MM_MEDIA_GAME_MOUNTPOINT}" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Unmounting ${MM_MEDIA_GAME_MOUNTPOINT}"
        /bin/umount "${MM_MEDIA_GAME_MOUNTPOINT}"
    fi
 
    if [ -n "${MM_MEDIA_SCREENSAVER_GALLERY_URL}" ] && [ -d "${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Unmounting ${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}"
        /bin/umount "${MM_MEDIA_SCREENSAVER_GALLERY_MOUNTPOINT}"
    fi

    if [ -f "/var/lib/minimyth.bootdir.cifs_mounted" ] || \
       [ -f "/var/lib/minimyth.bootdir.nfs_mounted" ] ; then
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/media] Unmounting '${MM_MINIMYTH_BOOT_URL}' share"
        /bin/umount "/var/minimyth.bootdir"
        /bin/rm -f "/var/lib/minimyth.bootdir.*"
    fi

  return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
